package com.ctgu.server.service.impl;

import com.ctgu.server.dao.entity.Article;
import com.ctgu.server.dao.entity.ArticleImage;
import com.ctgu.server.dao.vo.Result;
import com.ctgu.server.mapper.ArticleImageMapper;
import com.ctgu.server.service.ArticleImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.server.service.ArticleService;
import com.ctgu.server.utils.ResultUtil;
import com.ctgu.server.utils.oss.OssClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ctgu
 * @since 2023-05-24
 */
@Service
public class ArticleImageServiceImpl extends ServiceImpl<ArticleImageMapper, ArticleImage> implements ArticleImageService {
    @Autowired
    private ArticleService articleService;
    @Override
    public Result uploadImage(Long articleId, String targetUrl, MultipartFile multipartFile) {
        Article article= articleService.getById(articleId);
        if(article==null){
            return ResultUtil.error("参数错误:"+articleId);
        }
        //1.上传图片
       String imageUrl=  OssClientUtils.upload(multipartFile);
        //2.创建实体
        ArticleImage articleImage=new ArticleImage();
        articleImage.setUrl(targetUrl);
        articleImage.setArticleId(articleId);
        articleImage.setImageUrl(imageUrl);
        //3.保存实体到数据库
        this.save(articleImage);

        return ResultUtil.success(articleImage);
    }
}
